import sys
import time
from importlib import reload
from selenium.webdriver.support import expected_conditions as EC
import pyperclip
from selenium import webdriver
from selenium.webdriver.common.by import By
import pyautogui
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.common import exceptions
from selenium.webdriver.support.wait import WebDriverWait

import process


driver = webdriver.Chrome()
# 设置全屏
driver.maximize_window()
driver.implicitly_wait(30)
driver.get('http://106.53.17.186:8080/login')
# 保持现状2秒
# time.sleep(2)

# # 1.设置分辨率
# driver.set_window_size(1000,800)
# time.sleep(2)
# driver.close()

# # 2.同一页面的前进后退，back()和forward()
# driver.get('https://v220-area.21silkroad.com/login')
# time.sleep(2)
# driver.back()
# time.sleep(2)
# driver.forward()

# # 3.获取网站信息
# print('获取网站标题：'+driver.title)
# print('获取当前页面链接：'+driver.current_url)
# print('获取浏览器名称：'+driver.name)
# print('获取当前页面的源码：'+driver.page_source)

# # 4.定位页面元素find_element(by=By.ID,value=''),其中的ID可以换成CLASS_NAME/APATH/LIKE_TEXT/PARTIAL_LINK_TEXT/TAG_NAME/CSS_SELECTOR
name = driver.find_element(by=By.ID,value='account')
code = driver.find_element(by=By.ID,value='password')
button = driver.find_element(by=By.XPATH,value='/html/body/div[2]/div/user-layout/user-content/user-form-wrapper/div[2]/button')
name.send_keys('kpcbawuf')
code.send_keys('1234567')
button.click()
# print(len(driver.window_handles))
time.sleep(3)

# # 5.模拟鼠标操作ActionChains,context_click()/double_click()/double_and_drop()/move_to_element()/perform()
# ActionChains(driver).double_click(name).send_keys('10201020').perform()

# 6.模拟鼠标操作Keys,send_key(Keys.Back_SPACK),其中BACK_SPACE可以变为SPACK/TAB/ESCAPE/ENTER/CONTROL,'a'/CONTROL,'c'/CONTROL,'v'/CONTROL,'x'/F1
# rou3 = driver.find_element(by=By.XPATH,value='//*[@id="root"]/div/exporter-layout/div[3]/basic-layout-header/flex-row/div/div[1]')
# ActionChains(driver).click_and_hold(rou3).click().perform()
# rou3.click()
# rou = driver.current_url
# driver.get(rou)
# print(rou)
# rou1 = driver.find_element(by=By.XPATH,value='//*[@id="root"]/div/exporter-layout/div[2]/aside/div/ul/li[2]')
# rou1.click()
# # ActionChains(driver).click_and_hold(rou1).click().perform()
# rou2 = driver.find_element(By.XPATH,'//span[contains(.,"出口订单")]')
# rou2.click()
# ActionChains(driver).click_and_hold(rou2).click().perform()
# creatIP = driver.find_element(by=By.XPATH,value='//*[@id="root"]/div/exporter-layout/div[3]/div/div[5]/div/div/div/div[1]/div/div[1]/div[1]/div/div/div/span[1]')
# ActionChains(driver).move_to_element(creatIP).send_keys(Keys.CONTROL,'c').perform()
# search = driver.find_element(by=By.XPATH,value='//*[@id="keyQuery"]/span/input')
# ActionChains(driver).move_to_element(search).send_keys(Keys.CONTROL,'v').perform()


# # 7.下拉到指定元素位置scrollIntoView
# fengkong = driver.find_element(By.XPATH,'//*[@id="root"]/div/exporter-layout/div[3]/basic-layout-header/flex-row/div/div[10]/span')
# fengkong.click()
# target = driver.find_element(By.XPATH,'/html/body/div[3]/div/div[2]/div/div[2]/div/div[2]/div[3]/div/div/div/div[1]/div/div[9]/div[2]/div/div[1]/div/div')
# driver.execute_script("arguments[0].scrollIntoView();", target)


# # 8.def处理元素获取异常
# def except01():
#     try:
#         creatIP = driver.find_element(by=By.XPATH,value='//*[@id="root"]/div/exporter-layout/div[3]/div/div[5]/div/div/div/div[1]/div/div[1]/div[1]/div/div/div/span[1]')
#         return True
#     except exceptions.NoSuchElementException:
#         return False

# # 9.切换操作switch_to_windows(driver.window.handles)
# ai = driver.find_element(By.XPATH,'//*[@id="root"]/div/exporter-layout/ai')
# ai.click()
# print(len(driver.window_handles))
# print(driver.current_window_handle)
# print(driver.window_handles)
# driver.switch_to_window(driver.window_handles[-1])
# search1 = driver.find_element(By.XPATH,'//*[@id="message"]')
# search1.send_keys('如何退税')
# button1 = driver.find_element(By.XPATH,'//*[@id="submit-btn"]/div[1]')
# button1.click()
#
# iframe = driver.find_element(By.XPATH,'//*[@id="loginDiv"]/iframe')
# iframe_id = iframe.get_attribute('id')
# print('//*[@id="'+iframe_id+'"]')
# driver.switch_to_frame(iframe_id)
# email = driver.find_element(By.NAME, 'email')
# email.send_keys('1233@qq.com')
# driver.switch_to_parent_frame()
# driver.switch_to_default_content()

# # 10.cookie
# print(f'Cookies的值：{driver.get_cookies()}')
# print(driver.get_cookies())
# driver.add_cookie({'name':'小鱼','value':'帅哥'})
# print(driver.get_cookies())
# driver.delete_all_cookies()
# print(driver.get_cookies())

# # 11.判断元素是否存在
# def is_element_exist(driver,xpath):
#     try:
#         element = driver.find_element(By.XPATH,xpath)
#         flag=True
#     except:
#         flag=False
#     return flag
# if is_element_exist(driver,'//*[@id="keyQuery"]/span/input'):
#     print('存在元素')
# else:
#     print('不存在元素')

# # 12.xpath的高级用法，id定位、xpath搜索用法、class用法、xpath全路径法、属性name法
# def is_element_exist(driver,value):
#     try:
#         if value['key'] =='id':
#             element = driver.find_element(By.ID,value['value'])
#         elif value['key'] =='xpath':
#             element = driver.find_element(By.XPATH,value['value'])
#         elif value['key'] == 'class':
#             element = driver.find_elements(By.CLASS_NAME,value['value'])[0]
#         else:
#             print('元素输入定位条件错误，请从新输入')
#         falg = True
#     except:
#         print(value['value']+'不存在元素')
#         falg = False
#     return falg
# driver.get("https://v220-exporter.21silkroad.com/login")
# time.sleep(4)
# id_element = {'key':'id','value':'nameLogin'}
# xpath_element = {'key':'xpath','value':'//div[contains(.,"验证码登录")]'}
# all_xpath_element = {'key':'xpath','value':'//*[@id="rc-tabs-1-tab-code"]'}
# class_element = {'key':'class','value':'ant-tabs-tab-btn'}
# all_element ={'key':'xpath','value':'//div[@class="ant-tabs-tab-btn" and @aria-controls="rc-tabs-1-panel-code"]'}
# is_element_exist(driver,id_element)
# is_element_exist(driver,xpath_element)
# is_element_exist(driver,all_xpath_element)
# is_element_exist(driver,class_element)
# is_element_exist(driver,all_element)


# 13.弹窗switch_to_alert,accept(),dismiss()
# driver.get('file:///C:/Users/IT14/Desktop/sdsd.html')
#
# alert_button = driver.find_element(By.ID,'alert')
# alert_button.click()
# alert = driver.switch_to_alert()
# print(alert.text)
# time.sleep(2)
# alert.accept()

# confirm_button = driver.find_element(By.ID,'confirm')
# confirm_button.click()
# confirm = driver.switch_to_alert()
# print(confirm.text)
# time.sleep(2)
# confirm.dismiss()
#
# prompt_button = driver.find_element(By.ID,'prompt')
# prompt_button.click()
# prompt = driver.switch_to_alert()
# print(prompt.text)
# prompt.send_keys('jsdjksdkksdkkkdkskdkskdks')
# time.sleep(2)
# prompt.accept()

# 14.设置弹窗，输入内容
# driver.set_window_position(1024,1024,windowHandle='current')
# js = 'alert("弹窗提示！请选择出口类型");'
# driver.execute_script(js)
# driver.create_web_element()
#

# # 15.打开两个页面execute_script("window.open('{}','_blank');".format('https://v220-dsr.21silkroad.com/login'))
# driver.execute_script("window.open('{}','_blank');".format('https://v220-dsr.21silkroad.com/login'))

# # 16.上传图片pyperclip、pyautogui
# rou1 = driver.find_element(By.XPATH,'//span[contains(.,"商品库")]')
# rou1.click()
# # ActionChains(driver).click_and_hold(rou1).click().perform()
# rou2 = driver.find_element(By.XPATH,'//span[contains(.,"商品管理")]')
# rou2.click()
# rou3 = driver.find_element(By.XPATH,'//span[contains(.,"新增商品准入")]')
# rou3.click()
# time.sleep(3)
# rou5 = driver.find_element(By.XPATH,'//*[@id="nameGoods"]/span/input')
# rou5.send_keys('dshdhhs图片02323')
# rou4 = driver.find_element(By.XPATH,'//*[@id="FieldFormItems-Col-picGoods"]/div/div/div[2]/div[1]/div/div/span/div/div/span/button')
# rou4.click()
# time.sleep(2)
#
# pyperclip.copy(r'C:\Users\IT14\Desktop\图片01.jpg')
# time.sleep(1)
# pyautogui.hotkey('Ctrl','V')
# pyautogui.hotkey('enter')

# 17.等待sleep/implicitly_wait()/WebDriverWait(driver,10,1).until(EC.presence_of_element_located((id,value),message)
if WebDriverWait(driver,10,0.5).until(EC.presence_of_element_located((By.XPATH,'//span[contains(.,"出口订单")]')),'找不到该元素'):
    print('找到该元素！')

print('shdhs')

time.sleep(10)
driver.close()
driver.quit()